package red.sms.db.dao;

import inet.pdu.SMSRequest;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;

import red.sms.db.config.DBConfig;
import red.sms.db.entity.SmsMO;
import red.sms.util.Logger;
public class SmsMoDAO {
	public static DBConfig dbc = new DBConfig();
	
	//insert method
	public static void enqueue(SMSRequest smsRequest) {
		try {
			  java.util.Date date= new java.util.Date();
	          String insertSQL = "INSERT INTO sms_mo(request_id, phone_number, command_code, ma_dich_vu, info, update_date)" + "VALUES (?, ?, ?, ?, ?, ?)";
	          Connection con = ConnectionFactory.getInstance().getConnection();	 
	          PreparedStatement ps = con.prepareStatement(insertSQL);
	          ps.setString(1, smsRequest.getRequestId().toString());
	          ps.setString(2, smsRequest.getReceiverNumber());
	          ps.setString(3, smsRequest.getCommandCode());
	          ps.setString(4, smsRequest.getServiceNumber());
	          ps.setString(5, smsRequest.getInfo());
	          ps.setTimestamp(6, new Timestamp(date.getTime()));
	          ps.executeUpdate();
	          con.close();
	      } catch (Exception ex) {
	    	  Logger.log("\n"+ ex.getMessage(), true);
	      }
		
	}
	
	//delete method
	public void Delete(String request_id) {
	     try {
	    	  String deleteSQL = "DELETE from sms_mo WHERE request_id = ?";
	          Connection con = ConnectionFactory.getInstance().getConnection();
	          PreparedStatement ps = con.prepareStatement(deleteSQL);
	          ps.setString(1, request_id);
	          ps.executeUpdate();
	          con.close();
	      } catch (Exception ex) {
	    	  Logger.log("\n"+ ex.getMessage(), true);
	      }
	  }
	
		//getFirstRecord
		public SmsMO dequeue() {
		     try {
				   Connection con = ConnectionFactory.getInstance().getConnection();
				   Statement stmt = con.createStatement();
				   ResultSet result = stmt.executeQuery("SELECT * FROM sms_mo where active_flag = 1 ORDER BY update_date DESC LIMIT 1");
				   SmsMO smsMo = new SmsMO();
			          while(result.next())
			          {  
			             smsMo.setMoId(result.getInt("mo_id"));
			             smsMo.setRequestId(result.getString("request_id"));
			             smsMo.setInfo(result.getString("info"));
			             smsMo.setMaDichVu(result.getString("ma_dich_vu"));
			             smsMo.setPhoneNumber(result.getString("phone_number"));
			             smsMo.setCommandCode(result.getString("command_code"));
			          }
			          
			          if (smsMo.getRequestId() != null){
			    	   String updateSql = "UPDATE  sms_mo SET active_flag = 0 WHERE request_id = " + smsMo.getRequestId();
			    	   PreparedStatement ps = con.prepareStatement(updateSql);
			    	   ps.executeUpdate();
			       }
			           
			          con.close();
			          return smsMo;
		      } catch (Exception ex) {
		    	  ex.printStackTrace();
		    	  Logger.log("\n"+ ex.getMessage(), true);
		    	  return null;
		      }
		  }
	
	/*public static void main(String args[]){
		//SmsMoDAO.insert("1133", "0902264213","HT", "BBC", "sonph");
		SmsMoDAO son = new SmsMoDAO();
		SmsMO mo = son.dequeue();
	}*/
	
	public static void updateQueue(String request_id) {
	      try {
	    	  String updateSql = "UPDATE  sms_mo SET active_flag = 0 WHERE request_id = " + request_id;
	          Connection con = ConnectionFactory.getInstance().getConnection();
	          PreparedStatement ps = con.prepareStatement(updateSql);
	          ps.executeUpdate();
	          con.close();

	      } catch (Exception ex) {
	    	  Logger.log("\n"+ ex.getMessage(), true);
	      }
	  }
}

